package gestionParcours;

import modele.UniteEnseignement;
import modele.Bd;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JButton;

import javax.swing.table.TableModel;
import javax.swing.table.AbstractTableModel;

import javax.swing.border.BevelBorder;

import java.util.SortedSet;

import java.awt.BorderLayout;
import java.awt.Component;

public class UniteEnseignementAfficheAction
extends AbstractAction  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	GestionParcours appli;
	JScrollPane scrollpane;
	JPanel vue;
	TableModel dataModel;
	JTable uesListe;

	protected UniteEnseignementAfficheAction(GestionParcours appli) {
		super("UniteEnseignementAfficheAction");
		this.appli = appli;
		vue = appli.getUniteEnseignementAffiche();
	}

	private JTable getUniteEnseignementListe() throws Exception {
		return uesListe;
	}

	private UniteEnseignement getSelectedUniteEnseignement(int ind) throws Exception {
		SortedSet<UniteEnseignement> ues = appli.getBd().getUniteEnseignements();
		int i = 0;
		for (UniteEnseignement ue : ues) {
			if (i==ind)
				return ue;
			i++;
		}
		return null;
	}

	public UniteEnseignement getSelectedUniteEnseignement() throws Exception {
		if (getUniteEnseignementListe().getSelectedRow()!=-1)
			return getSelectedUniteEnseignement(getUniteEnseignementListe().
					getSelectedRow());
		else
			return null;
	}

	public Component getVue(){
		return vue;
	}

	public void actionPerformed(ActionEvent e) {
		try {
			if(vue == null) {
				vue = new JPanel();
				vue.setLayout(new BorderLayout());
				appli.add(vue,BorderLayout.CENTER);
				scrollpane = new JScrollPane();
				scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));
				vue.add(scrollpane,BorderLayout.CENTER);

				JPanel pied = new JPanel();
				vue.add(pied, BorderLayout.SOUTH);
				JButton ok = new JButton("Fermer");
				ok.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						vue.setVisible(false);
					}
				});
				pied.add(ok);
				JButton maj = new JButton("Details");
				maj.addActionListener(new UniteEnseignementMajAction(appli,this));
				pied.add(maj);
			}
			Bd bd = appli.getBd();
			final SortedSet<UniteEnseignement> ues = bd.getUniteEnseignements();
			final String[] names = UniteEnseignement.entete;
			final Object[][] data = new Object[ues.size()][names.length];
			int i = 0;
			for (UniteEnseignement ue : ues) {
				data[i] = ue.getObjet();
				i++;
			}
			// Create a model of the data.
			dataModel = new AbstractTableModel() {
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;
				// These methods always need to be implemented.
				public int getColumnCount() { return names.length; }
				public int getRowCount() { return ues.size();}
				public Object getValueAt(int row, int col) {
					return data[row][col];
				}
				public void setValueAt(Object aValue, int row, int column){
					data[row][column] = aValue;
				}
				public String getColumnName(int column) {
					return names[column];
				}
				@SuppressWarnings({ "unchecked", "rawtypes" })
				public Class getColumnClass(int c) {
					return getValueAt(0, c).getClass();
				}
				public boolean isCellEditable(int row, int col) {
					return (col < 4);
				}
			};
			// Create the table
			uesListe = new JTable(dataModel);
			uesListe.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
			//uesListe.setRowHeight(2*uesListe.getRowHeight());
			// Finish setting up the table.
			scrollpane.setViewportView(uesListe);
			vue.setVisible(true);
			appli.getFrame().setVisible(true);
		}
		catch(Exception ex){
			JOptionPane.showMessageDialog(appli, ex.getMessage(), "Affichage des UE : Attention",JOptionPane.ERROR_MESSAGE);
			ex.printStackTrace();
		}
	}
}

